<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\api\model\v1\AdminLog;
use app\api\model\v1\MonthReport;
use app\api\model\v1\Voluntary;
use app\BaseController;
use think\facade\Env;
use think\helper\Str;
use think\Request;

class VoluntaryCon extends BaseController
{


    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/13 15:06
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 志愿服务数据源
     */
    public function getVoluntaryList(){
        $limit = $this->request->param('limit', 10);
        $type  = $this->request->param('type', 0);
        $map = [];
        //获取整改管理员和超级管理员
        if(!$this->auth->isSuperAdmin($this->request->wx_id)){
            $group_id_arr = $this->auth->getGroups($this->request->wx_id)->toArray(); //获取当前角色的id
            $new_string = '';
            foreach ($group_id_arr as $k => $v) {
                $new_string .= $v['name'].',';
            }
            $new_string = rtrim($new_string,',');
            if(Str::contains($new_string,'整改管理员')){
                $childrenList = $this->search->getChildrenIds($this->request->wx_unit_id, true);
                $map[] = array('unit_id', 'in', $childrenList);
            }else{
                $map[] = ['admin_id', '=',$this->request->wx_id];
            }
        }

        if (intval($type) === 1){
            $map[] = ['admin_id', '=',$this->request->wx_id];
        }
        $res = Voluntary::with(['admin','unit'])->where($map)->paginate($limit)->each(function ($item,$key){
            if (empty($item['unit'])){
                $item['unit_title'] = '未知单位';
            }else{
                $item['unit_title'] = $item['unit']['title'];
            }

            if ($item['path']){
                $path_arr = json_decode($item['path'],true);
                $item['path_arr'] = reset($path_arr)['url'] ?? Env::get('IMG_HTTP.img_http').'logo.png';
            }else{
                $item['path_arr'] = Env::get('IMG_HTTP.img_http').'logo.png';
            }

            $item['type_name'] = Voluntary::$is_type[$item['type']] ?? '未知';
            $item['status_name'] = Voluntary::$is_status[$item['status']] ?? '未知';

            return $item;

        });
        return $this->response($res);
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * PC端获取志愿服务
     */
    public function getPcVoluntaryList(){
        $limit = $this->request->param('limit', 10);
        $res = Voluntary::with(['admin','unit'])->paginate($limit)->each(function ($item,$key){
            if (empty($item['unit'])){
                $item['unit_title'] = '未知单位';
            }else{
                $item['unit_title'] = $item['unit']['title'];
            }
            if ($item['path']){
                $item['path_arr'] = json_decode($item['path'],true);
            }else{
                $item['path_arr'] = Env::get('IMG_HTTP.img_http').'logo.png';
            }

            $item['type_name'] = Voluntary::$is_type[$item['type']] ?? '未知';
            $item['status_name'] = Voluntary::$is_status[$item['status']] ?? '未知';

            return $item;

        });
        return $this->response($res);
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/13 14:40
     * @return \think\response\Json
     * 保存志愿服务
     */
    public function saveVoluntary()
    {
        $data = $this->request->param();
        if (intval($data['type']) === 1) {
            foreach ($data['path'] as $key => $v) {
                $data['path'][$key]['thumb'] = $v['url'];
            }
            $data['path'] = json_encode($data['path'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); //不转义
        }
        $data['admin_id'] = $this->request->wx_id;
        $data['unit_id'] = $this->request->wx_unit_id;
        Voluntary::create($data);
        //操作日志
        AdminLog::record('上传志愿服务', $data, $this->request->wx_id, $this->request->wx_user_name);
        return $this->response();
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 审核志愿服务
     */
    public function auditVoluntary(){
        $id = $this->request->param('id', 0);
        $data = $this->request->param();
        if (intval($id) === 0) {
            Voluntary::create($data);
        } else {
            $data['status'] = Voluntary::IS_STATUS_TWO;
            $vo = Voluntary::find($id);
            $vo->save($data);
        }
        AdminLog::record(intval($id) === 0 ? '新增PC志愿服务' : '编辑OR审核志愿服务', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/13 16:09
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除志愿服务
     */
    public function delVoluntary(){
        $data = $this->request->param();

        $res = Voluntary::find($data['id']);
        if (is_null($res)) {
            return $this->response(403, '未找到该志愿服务');
        }
        $res->delete();
        AdminLog::record('删除志愿服务', $data, $this->request->wx_id, $this->request->wx_user_name);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/13 16:19
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 详情志愿服务
     */
    public function voluntaryDetail(){
        $data = $this->request->param();
        $res = Voluntary::find($data['voluntary_id']);
        if (is_null($res)) {
            return $this->response(403, '未找到该志愿服务');
        }
        $res['path_arr'] = json_decode($res['path'],true);
        return $this->response($res->toArray());
    }


    /** +++++++++++++++++ 月报表 ++++++++++++++++++++++ */

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 月报表-- 数据源
     * time 2022-01-14
     * 刘海龙
     */
    public function getMonthReport(){
        $limit = $this->request->param('limit', 10);
        $type  = $this->request->param('type', 0);
        $map = [];
        if (intval($type) === 1){
            $map[] = ['admin_id', '=',$this->request->wx_id];
        }
        $res = MonthReport::with(['admin'])->where($map)->paginate($limit)->each(function ($item,$key){
            $path_arr = json_decode($item['path'],true);
            $item['path_arr'] = reset($path_arr)['url'] ?? Env::get('IMG_HTTP.img_http').'logo.png';
            $item['arr'] = $path_arr;
            $item['is_hot_name'] = MonthReport::$is_hot[$item['is_hot']] ?? '未知';
            $item['status_name'] = MonthReport::$is_status[$item['status']] ?? '未知';
            return $item;
        });
        return $this->response($res);
    }


    /**
     * @return \think\response\Json
     * 月报表 保存
     * time 2022-01-14
     * 刘海龙
     */
    public function saveMonthReport(){
        $data = $this->request->param();
        foreach ($data['path'] as $key => $v) {
            $data['path'][$key]['thumb'] = $v['url'];
        }
        $data['path'] = json_encode($data['path'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); //不转义
        $data['admin_id'] = $this->request->wx_id;
        $data['unit_id'] = $this->request->wx_unit_id;
        MonthReport::create($data);
        //操作日志
        AdminLog::record('上传月报表', $data, $this->request->wx_id, $this->request->wx_user_name);
        return $this->response();
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 审核月报表
     */
    public function auditMonthReport(){
        $id = $this->request->param('id', 0);
        $data = $this->request->param();
        if (intval($id) === 0) {
            MonthReport::create($data);
        } else {
            $data['status'] = MonthReport::IS_STATUS_TWO;
            $vo = MonthReport::find($id);
            $vo->save($data);
        }
        AdminLog::record(intval($id) === 0 ? '新增PC月报表' : '编辑OR审核月报表', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * @return \think\response\Json
     * 月报表 删除
     * time 2022-01-14
     * 刘海龙
     */
    public function delMonthReport(){
        $data = $this->request->param();

        $res = MonthReport::find($data['id']);
        if (is_null($res)) {
            return $this->response(403, '未找到该月报表');
        }
        $res->delete();
        AdminLog::record('删除月报表', $data, $this->request->wx_id, $this->request->wx_user_name);
        return $this->response();
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 月报表 详情
     * time 2022-01-14
     * 刘海龙
     */
    public function reportDetail(){
        $data = $this->request->param();
        $res = MonthReport::find($data['id']);
        if (is_null($res)) {
            return $this->response(403, '未找到该志愿服务');
        }
        $res['path_arr'] = json_decode($res['path'],true);
        return $this->response($res->toArray());
    }
}
